using System;
using Entities;
using System.Configuration;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;

namespace DAL
{
    public class tbHinhAnhDAL
    {
        private SqlConnection cn = null;
        public tbHinhAnhDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
        public void Insert(tbHinhAnhEntities hinhanh)
        {
            SqlCommand cm = new SqlCommand("sp_tbHinhAnh_Insert", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;         
            cm.Parameters.AddWithValue("@HinhAnh", hinhanh.HinhAnh);
            cm.Parameters.AddWithValue("@MaSP", hinhanh.MaSP);
            cm.ExecuteNonQuery();
        }
        public void Update(tbHinhAnhEntities hinhanh)
        {
            SqlCommand cm = new SqlCommand("sp_tbHinhAnh_Update", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@STT", hinhanh.STT);
            cm.Parameters.AddWithValue("@HinhAnh", hinhanh.HinhAnh);
            cm.Parameters.AddWithValue("@MaSP", hinhanh.MaSP);
            cm.ExecuteNonQuery();
        }
        public void Delete(int ID)
        {
            SqlCommand cm = new SqlCommand("sp_tbHinhAnh_Delete", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@STT", ID);
            cm.ExecuteNonQuery();
        }
        public tbHinhAnhEntities.tbHinhAnhEntitiesCollection Get(string where, int pageIndex,
            int pageSize, string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("sp_tbHinhAnh_Get", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection",orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", System.Data.DbType.Int32)).Direction = System.Data.ParameterDirection.Output;
            SqlDataReader dr = cm.ExecuteReader();
            tbHinhAnhEntities.tbHinhAnhEntitiesCollection HinhAnhCollection = new tbHinhAnhEntities.tbHinhAnhEntitiesCollection();
            while (dr.Read())
            {
                tbHinhAnhEntities ha = new tbHinhAnhEntities();
                ha.STT = int.Parse(dr.GetValue(0).ToString());
                ha.HinhAnh = dr.GetValue(1).ToString();
                ha.MaSP = int.Parse(dr.GetValue(2).ToString());
                HinhAnhCollection.Add(ha);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return HinhAnhCollection;

        }
        public int imageFirst(int IdSanPham)
        {
            string sql = "select Min(STT) from tbHinhAnh as H where H.MaSP='" + IdSanPham + "'";
            SqlCommand cm = new SqlCommand(sql, cn);
            int kq = (int)cm.ExecuteScalar();
            return kq;
        }
    }
}
